package puppy.eval.logloaders;

import java.util.ArrayList;
import java.util.Hashtable;

import util.io.TextFileLoader;
import fileloaders.extractors.LineFieldExtractor;
import fileloaders.filters.TextLineFilter;

public class QueryPairsLoader {

	TextFileLoader loader = null;
	TextLineFilter filter = null;
	LineFieldExtractor extractor = null;

	private Hashtable<String, Hashtable<String, ArrayList<String>>> gold = new Hashtable<String, Hashtable<String, ArrayList<String>>>();

	public QueryPairsLoader(String path, TextLineFilter filter,
			LineFieldExtractor extractor) {

		this.filter = filter;
		this.extractor = extractor;
		loader = new TextFileLoader(path);

	}

	public Hashtable<String, Hashtable<String, ArrayList<String>>> loadQueryPairs() {

		while (loader.hasNext()) {

			String line[] = loader.next();

			if (filter.filterLine(line)) {

				continue;
			}

			String query = extractor.getQuery(line);
			String reformulation = extractor.getReformulation(line);
			String type = extractor.getAge(line);

			Hashtable<String, ArrayList<String>> temp = null;

			if (gold.containsKey(type)) {

				temp = gold.get(type);

			} else {

				temp = new Hashtable<String, ArrayList<String>>();

			}

			ArrayList<String> list = new ArrayList<String>();
			if (temp.containsKey(query)) {
				list = temp.get(query);

			}

			list.add(reformulation);
			//System.out.println("loading " + query + "\t"+ reformulation);
			temp.put(query, list);
			gold.put(type, temp);

		}

		return gold;

	}

}
